package com.wmz.crm.workbench.controller;

import com.wmz.crm.commons.constants.Constants;
import com.wmz.crm.commons.entity.ReturnObject;
import com.wmz.crm.commons.utils.DateFormateUtil;
import com.wmz.crm.commons.utils.UUIDUtil;
import com.wmz.crm.settings.entity.User;
import com.wmz.crm.workbench.entity.Activity;
import com.wmz.crm.workbench.entity.ActivityRemark;
import com.wmz.crm.workbench.service.ActivityRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class ActivityRemarkController {

    @Autowired
    private ActivityRemarkService activityRemarkService;

    @RequestMapping("/workbench/activity/saveCreateActivityRemark.do")
    @ResponseBody
    public Object saveCreateActivityRemark(ActivityRemark remark, HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
        //封装参数
        remark.setId(UUIDUtil.getUUid());
        remark.setCreateTime(DateFormateUtil.formateDateTime(new Date()));
        remark.setCreateBy(user.getId());
        remark.setEditFlag(Constants.REMARK_EDIT_FLAG_NO_EDITED);
        try {
            //调用service层方法，保存创建的市场活动备注
            int ret = activityRemarkService.saveCreateActivityRemark(remark);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetObj(remark);
            }else {
                returnObject.setMessage("系统忙，请稍后重试……");
            }
        }catch (Exception e) {
            returnObject.setMessage("系统忙，请稍后重试……");
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/activity/deleteActivityRemarkById.do")
    @ResponseBody
    public Object deleteActivityRemarkById(String id) {
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        try {
            //调用service层方法，删除备注
            int ret = activityRemarkService.deleteActivityRemarkById(id);
            if(ret < 0) {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后再试……");
            }
        } catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后再试……");
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/activity/saveEditActivityRemark.do")
    @ResponseBody
    public Object saveEditActivityRemark(ActivityRemark remark,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        User user = (User)session.getAttribute(Constants.SESSION_USER);
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
        //获取参数并封装
        remark.setEditTime(DateFormateUtil.formateDateTime(new Date()));
        remark.setEditFlag(Constants.REMARK_EDIT_FLAG_YES_EDITED);
        remark.setEditBy(user.getId());
        try {
            //调用service层方法，更新修改的市场活动备注；
            int ret = activityRemarkService.saveEditActivityRemark(remark);
            if(ret > 0) {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetObj(remark);
            }else {
                returnObject.setMessage("系统忙， 请稍后重试……");
            }
        }catch (Exception e) {
            returnObject.setMessage("系统忙， 请稍后重试……");
            e.printStackTrace();
        }
        return returnObject;
    }
}
